package cn.wsalix.admin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.anlaser.service.impl.CommonServiceImpl;
import cn.anlaser.utils.SearchFilter;
import cn.anlaser.utils.SearchFilter.Operator;
import cn.wsalix.admin.entity.AuthData;
import cn.wsalix.admin.form.AuthDataForm;
import cn.wsalix.admin.repository.AuthDataRepository;
import cn.wsalix.admin.repository.MenuPathRepository;
import cn.wsalix.admin.service.AuthDataService;
import cn.wsalix.admin.service.MenuService;
import cn.wsalix.constant.Global;
import cn.wsalix.exception.ExistException;

@Service("AuthDataService")
public class AuthDataServiceImpl
		extends
		CommonServiceImpl<AuthDataForm, AuthDataRepository, AuthData>
		implements AuthDataService {
	@Autowired
	private AuthDataRepository AuthDataRepository;
	
	@Autowired
	private MenuPathRepository MenuPathRepository;
	@Autowired
	private ExistException existException;
	@Autowired
	private MenuService menuService;

	@Override
	public String module() {
		return "/admin/requires_role";
	}

	@Override
	public AuthDataRepository getRepository() {
		return AuthDataRepository;
	}

	@Override
	public Class<AuthData> getEntityClazz() {
		return AuthData.class;
	}

	@Transactional
	@Override
	public AuthData add(AuthDataForm form) {
		if (isExistByCode(form.getCode())) {
			throw Global.getException("",existException.getCodeExist());
		}
		return super.add(form);
	}

	@Transactional
	@Override
	public AuthData edit(AuthDataForm form) {
		if (isExistByCode(form.getCode())) {
			throw Global.getException("",existException.getCodeExist());
		}
		return super.edit(form);
	}

	@Override
	public AuthData findByCodeOne(String code) {
		logger.info(code);
		List<AuthData> menus = findsByCode(code);
		if (menus.size() != 1) {
			if (menus.size() > 1) {
				logger.error(code + " : size() > 1 [" + menus.size() + "]");
				return menus.get(0);
			}
			logger.error(code + " : size() =0 ");
			return null;
		} else {
			return menus.get(0);
		}
	}

	@Override
	public List<AuthData> findsByCode(String code) {
		return AuthDataRepository.findByCode(code);
	}

	private boolean isExistByCode(String code) {
		AuthDataForm form = new AuthDataForm();
		form.setCode(code);
		List<AuthData> AuthDatas = findByForm(form);
		return !AuthDatas.isEmpty() && AuthDatas.size() > 0;
	}

	@Override
	public Map<String, SearchFilter> buildByForm(AuthDataForm form) {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		if (form.getCode() != null && !form.getCode().equals("")) {
			filters.put("code",
					new SearchFilter("code", Operator.EQ, form.getCode()));
		}

		return filters;
	}

	@Override
	public List<AuthData> findAuthDatas() {
		return AuthDataRepository.findAll();
	}

	@Override
	public List<AuthData> findRolesByUserId(Long userId) {
		return MenuPathRepository.findRolesByUserId(userId);
	}

}
